package com.cq.hd.common.pagination;

import com.cq.hd.common.constant.Constant;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description: 分页对象
 * @author: mYunYu
 * @date: 2021/7/7 15:57
 * @version: v1.0
 */
@Data
@NoArgsConstructor
public class Page<T> {

    /**
     * 具体的数据
     */
    private List<T> list;


    /**
     * 当前页
     */
    private int pageNum;

    /**
     * 每页数据量
     */
    private int pageSize;

    /**
     * 总记录数
     */
    private long total;

    /**
     * 总页数
     */
    private int pages;


    public static <T> Page<T> newPageWithAllFields(List<T> list, int pageNum, int pageSize, long total,
                                                   int pages) {
        Page<T> page = new Page<>();

        page.setList(list);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }

    public static <T> Page<T> empty(int pageNum, int pageSize) {
        Page<T> page = new Page<>();
        page.setList(Collections.emptyList());
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTotal(0);
        page.setPages(0);
        return page;
    }

    public static <T> Page<T> newDefault() {
        Page<T> page = new Page<>();
        page.setList(new ArrayList<>());
        page.setPageNum(Constant.DEFAULT_PAGE_NUM);
        page.setPageSize(Constant.DEFAULT_PAGE_SIZE);
        page.setTotal(0);
        page.setPages(1);
        return page;
    }

}
